package levelrewind.android.app.model;

import java.util.ArrayList;
import java.util.Date;

import levelrewind.android.app.R;
import levelrewind.android.app.vo.PersonScore;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;

public class LineChart {
	private GraphicalView mChartView; 
	private Context context;
	private TimeSeries series;

	private XYMultipleSeriesDataset dataset;
	private String xTitle;
	private String yTitle;
	

	public LineChart(Context c) {
		context = c;
		
		dataset = new XYMultipleSeriesDataset();
	}
	
	// Create person's chart
	public GraphicalView createChart(PersonScore[] scores) {
		series = new TimeSeries("");
		for (int i=0; i<scores.length;i++){
			String week = String.valueOf(scores[i].getWeek()).substring(4);
			series.add(Double.parseDouble(week), scores[i].getScore());
		}
		
		xTitle = "Time";
		yTitle = "Score";
		XYMultipleSeriesRenderer mRenderer = createChart();
		mChartView = ChartFactory.getLineChartView(context, dataset, mRenderer);
		return mChartView;
	}
	
	// Create battle's chart
	public GraphicalView createChart(ArrayList<Date> dList, ArrayList<Long> scores ) {
		series = new TimeSeries("");
		for (int i=0; i<scores.size();i++){
			series.add(dList.get(i), scores.get(i));
		}
		
		xTitle = "Time";
		yTitle = "Score";
		
		XYMultipleSeriesRenderer mRenderer = createChart();
//		mRenderer.setXLabelsAngle(50);
		mChartView = ChartFactory.getTimeChartView(context, dataset, mRenderer, "MM/dd");
		
		return mChartView;
	}
	
	private XYMultipleSeriesRenderer createChart() {

		dataset.addSeries(series);
		
		XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
		
		mRenderer.setApplyBackgroundColor(true);
	    mRenderer.setBackgroundColor(context.getResources().getColor(R.color.blackBackground));
	    mRenderer.setMarginsColor(context.getResources().getColor(R.color.blackBackground));
	    mRenderer.setAxesColor(Color.GRAY);
	    mRenderer.setLabelsColor(Color.DKGRAY);
	    mRenderer.setXLabelsColor(Color.WHITE);
	    mRenderer.setYLabelsColor(0, Color.WHITE);
	    mRenderer.setXTitle(xTitle);
	    mRenderer.setYTitle(yTitle);
	    mRenderer.setShowLabels(false);
	    
	    mRenderer.setShowLegend(false);
	    mRenderer.setPanEnabled(false, false);
	    mRenderer.setZoomEnabled(false, false);
	    
		XYSeriesRenderer renderer = new XYSeriesRenderer();
		renderer.setColor(context.getResources().getColor(R.color.ltblue));
		renderer.setFillBelowLine(true);
		renderer.setFillBelowLineColor(context.getResources().getColor(R.color.blueTextColor));
		mRenderer.addSeriesRenderer(renderer);
		
		return mRenderer;
	}
	
}
